package com.ebt.m.wiki;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.ebt.m.AppContext;
import com.ebt.m.R;
import com.ebt.m.data.bean.Brand;
import com.ebt.m.data.rxModel.apibean.ProductSearchBean;
import com.ebt.m.data.rxModel.apibean.SearchHistory;
import com.ebt.m.utils.DataValidation;
import com.ebt.m.utils.ThemeUtil;
import com.ebt.m.view.ClearEditText;
import com.ebt.m.widget.BaseActivity;
import com.ebt.m.wiki.event.EFilter;
import com.ebt.m.wiki.view.SpecProductLibSearchListView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 专属产品库搜索页面
 */
public class ActSpecProductLibSearch extends BaseActivity {

    @BindView(R.id.btn_cancel)
    FrameLayout btnCancel;
    @BindView(R.id.btn_search)
    FrameLayout btnSearch;
    @BindView(R.id.search_view)
    ClearEditText searchView;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.content)
    FrameLayout content;
    @BindView(R.id.progress_bar)
    ProgressBar mProgressBar;
    private SpecProductFilterHistory specProductLibSearchHistorys;
    private SpecProductLibSearchListView searchListView;
    private int mFrom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeUtil.setMeizuStatusBarDarkIcon(this, true);
        ThemeUtil.setMiuiStatusBarDarkMode(this, true);
        setContentView(R.layout.product_spec_lib_search);
        ButterKnife.bind(this);
        initViews();
        initData();
    }

    private void initData() {
        mFrom = getIntent().getIntExtra("Flag", 0);
        content.removeAllViews();
        specProductLibSearchHistorys = new SpecProductFilterHistory(mContext);
        content.addView(specProductLibSearchHistorys);
        specProductLibSearchHistorys.update(2);
        specProductLibSearchHistorys.setOnHistoryItemClick(new SpecProductFilterHistory.OnHistoryItemClick() {
            @Override
            public void onItemClick(Brand brand, int position) {
                if (brand == null) return;
                searchView.setText(brand.getBrandShortName());
                btnSearch.performClick();
            }
        });
    }

    @Override
    public void initViews() {
        super.initViews();
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        EventBus.getDefault().post(new EFilter(null));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void search() {
        String keyword = searchView.getText().toString().trim();
        if (DataValidation.isEmpty(keyword)) {
            toast("请输入搜索条件！");
            return;
        }
        searchListView = new SpecProductLibSearchListView(getContext());
        searchListView.update(generaterSelectionParams());
        searchListView.addFlag(mFrom);
        content.removeAllViews();
        content.addView(searchListView);
        closeInput();
    }

    private ProductSearchBean generaterSelectionParams() {
        ProductSearchBean searchBean = new ProductSearchBean();
        searchBean.setKeyword(searchView.getText().toString().trim());
        searchBean.setCompanyId(AppContext.getCurrentUser().getCompanyInfo().getCompanyID());
        searchBean.setRequestFrom(2);
        return searchBean;
    }

    @OnClick({R.id.btn_cancel, R.id.btn_search, R.id.search_view})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                closeInput();
                finish();
                break;
            case R.id.btn_search:
                search();
                break;
        }
    }
}
